package dao;

import utils.DBHelper;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EmployeeDao {

    public Map<String, Object> query(String position, String department, int page, int size) {
        String sql = "select * from employee where status=1";
        List<Object> params = new ArrayList<>();

        if (position != null && position.isEmpty() == false){
            sql += " and position like ? ";
            params.add("%" + position + "%");
        }
        if (department != null && department.isEmpty() == false){
            sql += " and department like ?";
            params.add("%" + department + "%");
        }
        long total = DBHelper.count(sql, params.toArray());

        sql += " limit ?, ?";
        params.add(page);
        params.add(size);
        List<Map<String, Object>> data = DBHelper.selectListMap(sql, params.toArray());
        Map<String,Object> res = new HashMap<>();
        res.put("total", total);
        res.put("data", data);
        return res;
    }
    public int exists(String telphone, String name){
        String sql = "select name from employee where telphone = ? and name = ?";
        if (DBHelper.selectOneMap(sql, telphone, name) == null){
            return 0;
            // 返回 0 则表示不存在数据
        }
        return 1;
        // 返回 1 则表示存在
    }


    // 查看员工是否存在
    public int existsEmp(String name, String email){
        String sql = "select name from employee where name = ? and email = ?";
        Map<String, Object> map = DBHelper.selectOneMap(sql, name, email);
        if(map == null){
            return 0;
        }
        return 1;
    }

    public void create(String name, String position, String entryTime, String salary, String department, String telphone){
        String sql = "insert into employee (name, position,manageId, entryTime, salary, department, telphone)" +
                " values(?, ?, 1, ?, ?, ?, ?)";
        DBHelper.update(sql, name, position, Date.valueOf(entryTime), salary, department, telphone);
    }

    public void modify(String id, String name, String position, String entryTime, String salary, String department, String telphone){
        String sql = "update employee set name=?, position=?, entryTime=?, salary=?, department=?, telphone=? where id=?";
        DBHelper.update(sql,name, position, Date.valueOf(entryTime), salary, department, telphone, id);
    }

    public void remove(String id){
        String sql = "delete from employee where id=?";
        DBHelper.update(sql, id);
    }
    public void remove1(String id){
        String sql = "update employee set status=0 where id=?";
        DBHelper.update(sql,id);
    }

    // 修改员工密码
    public void modifyPwd(String name, String password,String email){
        String sql = "update employee set telphone = ? where name = ? and email like ?";
        DBHelper.update(sql, password, name, "%" + email + "%");
    }

}
